package com.example.Administrator.shoujiweishi.activities;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.example.Administrator.shoujiweishi.R;
import com.example.Administrator.shoujiweishi.activities.base.BaseActivity;
import com.example.Administrator.shoujiweishi.services.IVersionUpData;
import com.example.Administrator.shoujiweishi.vo.VersionInfo;

import java.io.IOException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UpData_Version extends BaseActivity {

    private View view;
    private StringBuffer string=new StringBuffer();
    private TextView textView;
    private boolean aBoolean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getContentXML() {
        return R.layout.updata_version;
    }

    @Override
    public void initData() {
        view=findViewById(R.id.view_dialog);
        textView= (TextView) findViewById(R.id.text_up);

    }
    public void iniy() throws IOException {
        boolean b=checkNet();
        if (b){
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://www.babyiloveyou.ml/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            IVersionUpData service = retrofit.create(IVersionUpData.class);
            Call<VersionInfo> call=service.getVersionInfo();
            call.enqueue(new Callback<VersionInfo>() {
                @Override
                public void onResponse(Call<VersionInfo> call, Response<VersionInfo> response) {
                    //网络正常   可以下载~~~已经是最新版本
                    Toast.makeText(UpData_Version.this, "下载！！！", Toast.LENGTH_SHORT).show();
                    VersionInfo versionInfo=response.body();
                    int netcode=versionInfo.getVersionCode();
                    int oldcode=getVersionCode(getApplicationContext());
                    if(netcode>oldcode){
                        Intent intent=new Intent();
                        intent.putExtra("VersionInfo",versionInfo);
                        setResult(3,intent);
                        finish();
                    }else{
                        setResult(4);
                        finish();
                    }


                }

                @Override
                public void onFailure(Call<VersionInfo> call, Throwable t) {
                    //网络比较慢
                    setResult(4);
                    finish();
                }
            });
        }else{
            finish();
        }
    }
    public static int getVersionCode(Context context)//获取版本号(内部识别号)
    {
        try {
            PackageInfo pi=context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
    }

    public boolean checkNet(){
        NetworkInfo.State wifiState = null;
        NetworkInfo.State mobileState = null;
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        if (wifiState != null && mobileState != null && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED == mobileState) {
            // 手机网络连接成功
//            Toast.makeText(this, "手机网络连接成功", Toast.LENGTH_SHORT).show();
            setResult(1);
            return true;
        } else if (wifiState != null && mobileState != null
                && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED != mobileState) {
            // 手机没有任何的网络
//            Toast.makeText(this, "手机没有任何的网络", Toast.LENGTH_SHORT).show();
            setResult(2);
            return false;
        } else if (wifiState != null && NetworkInfo.State.CONNECTED == wifiState) {
            // 无线网络连接成功
//            Toast.makeText(this, "无线网络连接成功", Toast.LENGTH_SHORT).show();
            setResult(3);
            return true;
        }
        return false;


    }
    @Override
    public void initView() {

        final AnimatorSet animatorSet=new AnimatorSet();
        ObjectAnimator scaleX1=ObjectAnimator.ofFloat(view,"scaleX", 1f, 0f);
        ObjectAnimator scaleY1=ObjectAnimator.ofFloat(view,"scaleY", 1f, 0f);
        ObjectAnimator scaleX2=ObjectAnimator.ofFloat(view,"scaleX", 0f, 1f);
        ObjectAnimator scaleY2=ObjectAnimator.ofFloat(view,"scaleY", 0f, 1f);
        animatorSet.setDuration(2000);
        animatorSet.play(scaleX2).with(scaleY2).before(scaleX1).before(scaleY1);//两个动画同时开始
        animatorSet.start();
        string.append(".");
        textView.setText("检查网络ing"+ string.toString());
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                view.setEnabled(aBoolean);
                aBoolean=!aBoolean;
                ObjectAnimator scaleX1=ObjectAnimator.ofFloat(view,"scaleX", 1f, 0f);
                ObjectAnimator scaleY1=ObjectAnimator.ofFloat(view,"scaleY", 1f, 0f);
                ObjectAnimator scaleX2=ObjectAnimator.ofFloat(view,"scaleX", 0f, 1f);
                ObjectAnimator scaleY2=ObjectAnimator.ofFloat(view,"scaleY", 0f, 1f);
                animatorSet.setDuration(2000);
                animatorSet.play(scaleX2).with(scaleY2).before(scaleX1).before(scaleY1);//两个动画同时开始
                animatorSet.start();
                string.append(".");
                if(string.toString().length()>3){
                    string.delete(0,string.toString().length());
                }
                textView.setText("检查网络ing"+ string.toString());
            }
        });
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        },6000);
        try {
            iniy();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
